//
//  SkusModel.swift
//
//  Created by eme on 2016/12/17
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import NetWorkCore
import ModelProtocol

public class SkusModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kSkusPostageKey: String = "postage"
    internal let kSkusPriceKey: String = "price"
    internal let kSkusSkunameKey: String = "skuname"
    internal let kSkusStockKey: String = "stock"
    internal let kSkusFarpostageKey: String = "farpostage"
    internal let kSkusUnitKey: String = "unit"
    internal let kSkusMoqKey: String = "moq"
    internal let kSkusSkuidKey: String = "skuid"
    
    
    // MARK: 属性
    public var postage: Int
    public var price: Double
    public var skuname: String
    public var stock: Int
    public var farpostage: Int
    public var unit: String
    public var moq: Int
    public var skuid: String
    public var skus: [SkusModel]
    
    ///是否启用混批
    public var isHp:Bool = false
    ///计算时的起批量
    public var edit_moq:Int{
        get{
            if isHp {
                return 1
            }else{
                return self.moq
            }
        }
    }
    
    public var editnum:Int = 0
    public var totalNum:Int{
        get{
            return  self.editnum * self.edit_moq
        }
    }
    public var itemPrice:Double{
        get{
            return Double(self.totalNum) * self.price
        }
    }
    /// 主要用于显示 底部的价格
    public var totalPrice:Double{
        get{
            var relPrice:Double = 0.0
            for subItem in self.skus {
                relPrice = relPrice + subItem.itemPrice
            }
            return relPrice
        }
    }
    /// 主要用于底部显示已选数量
    public var totalEditNum:Int{
        get{
            var relNum:Int = 0
            for subItem in self.skus {
                relNum = relNum + subItem.totalNum
            }
            return relNum
        }
        
    }
    
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    public required  init?(json: JSON) {
        postage = json[kSkusPostageKey].intValue
        price = json[kSkusPriceKey].doubleValue
        skuname = json[kSkusSkunameKey].stringValue
        stock = json[kSkusStockKey].intValue
        farpostage = json[kSkusFarpostageKey].intValue
        unit = json[kSkusUnitKey].stringValue
        moq = json[kSkusMoqKey].intValue
        skuid = json[kSkusSkuidKey].stringValue
        skus = []
        if let items = json["skus"].array {
            for item in items {
                if let skusModel = SkusModel(json: item){
                    skus.append(skusModel)
                }
            }
        } else {
            skus = []
        }
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    public func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        dictionary.updateValue(postage as AnyObject, forKey: kSkusPostageKey)
        dictionary.updateValue(price as AnyObject, forKey: kSkusPriceKey)
        dictionary.updateValue(skuname as AnyObject, forKey: kSkusSkunameKey)
        dictionary.updateValue(stock as AnyObject, forKey: kSkusStockKey)
        dictionary.updateValue(farpostage as AnyObject, forKey: kSkusFarpostageKey)
        dictionary.updateValue(unit as AnyObject, forKey: kSkusUnitKey)
        dictionary.updateValue(moq as AnyObject, forKey: kSkusMoqKey)
        dictionary.updateValue(skuid as AnyObject, forKey: kSkusSkuidKey)
        
        
        return dictionary
    }
    
    
}
